package com.main.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.main.service.handler.ServiceHandlerProcessor;
import com.main.ui.model.Person;

@Controller
public class PersonController {
	
	@Autowired
	private ServiceHandlerProcessor serviceProcessor;
	
	@RequestMapping(method=RequestMethod.GET, value="/home")
	public String baseRedirect(){
		return "person.html";
	}
	
	@RequestMapping(method=RequestMethod.GET, value="/person/get")
	public @ResponseBody List<Person> getPersons(){
		return serviceProcessor.getService().getAllPersons();
	}
	
	@RequestMapping(method=RequestMethod.GET, value="/person/{personId}")
	public @ResponseBody Person getPerson(@PathVariable Long personId){
		return serviceProcessor.getService().getPersonDetails(personId);
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/person", consumes = "application/json")
	public @ResponseBody List<Person> savePerson(@RequestBody Person person){
		serviceProcessor.getService().savePersonDetails(person);
		return serviceProcessor.getService().getAllPersons();
	}
	
	@RequestMapping(method=RequestMethod.PUT, value="/person/{personId}", consumes = "application/json")
	public @ResponseBody List<Person> updatePerson(@RequestBody Person person){
		serviceProcessor.getService().updatePersonDetails(person);
		return serviceProcessor.getService().getAllPersons();
	}
	
	@RequestMapping(method=RequestMethod.DELETE, value="/person/{personId}")
	public @ResponseBody List<Person> deletePerson(@PathVariable Long personId){
		serviceProcessor.getService().deletePerson(personId);
		return serviceProcessor.getService().getAllPersons();
	}
	
}
